package com.bjpowernode.springboot.service.impl;

import com.bjpowernode.springboot.domain.Student;
import com.bjpowernode.springboot.mapper.StudentMapper;
import com.bjpowernode.springboot.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * CreateTime：2020/9/2 0:14
 */


@Service
public class StudentServiceImpl implements StudentService {

    @Autowired
    private StudentMapper studentMapper;

    @Autowired
    private RedisTemplate redisTemplate;


    @Override
    public List<Student> getStudentList() {

        //从redis中获取数据
        List student = redisTemplate.boundListOps("student").range(0, -1);

        if(student == null || student.size() <= 0){ //表示没有从redis中获取到数据

            //从mysql中获取数据
            List<Student> students = studentMapper.selectByExample(null);

            //将数据存入到redis中
            redisTemplate.opsForList().leftPushAll("student",students);

            System.out.println("从mysql中查询数据");

            return students;
        }else{

            System.out.println("从redis中查询数据");
            return student;
        }

    }
}
